package com.grape.juc.thread.stop;
/**
 * StopThread 使用中间变量停止线程
 *
 * @date: 2021/1/13 21:02 
 * @author: myx 
 * @version: V0.1
 * Copyright: 2020 serve51. All rights reserved. 
 */
public class StopThread extends Thread {
    //保证可见性
    private volatile boolean flag = true;

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
        while (flag) {

        }
    }

    /**
     * 停止线程
     */
    public void stopThread() {
        this.flag = false;
    }


    public static void main(String[] args) {
        StopThread thread005 = new StopThread();
        thread005.start();
        try {
            Thread.sleep(3000);
            thread005.stopThread();
        } catch (Exception e) {
        }
    }
}